package shortlink.mq.idempotent;


import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/*消息队列幂等处理器*/
@Component
@RequiredArgsConstructor
public class MessageQueueIdempotentHandler {

    private final StringRedisTemplate stringRedisTemplate;

    private final String IDEMPOTENT_KEY_PREFIX="short-link:idempotent:";

    /*判断消息是否被消费过*/
    public  boolean isMessageBeingConsumed(String messageId){
        String key=IDEMPOTENT_KEY_PREFIX+messageId;
        return Boolean.FALSE.equals(stringRedisTemplate.opsForValue().setIfAbsent(key,"0",2, TimeUnit.MINUTES));
    }

    /*设置消息流程执行完成*/
    public void setAccomplish(String messageId){
        String key=IDEMPOTENT_KEY_PREFIX+messageId;
        stringRedisTemplate.opsForValue().set(key,"1",2,TimeUnit.MINUTES);
    }

    /*判断消息消费流程是否完成*/
    public boolean isAccomplish(String messageId){
        String key=IDEMPOTENT_KEY_PREFIX+messageId;
        return Objects.equals(stringRedisTemplate.opsForValue().get(key),"1");
    }

    /*如果消息处理遇到异常删除幂等表示*/
    public void delMessageProcessed(String messageId){
        String key=IDEMPOTENT_KEY_PREFIX+messageId;
        stringRedisTemplate.delete(key);
    }
}
